# frozen_string_literal: true
class Customer::DoorsController < Customer::ApplicationController

  before_action :authenticate_user_by_token
  before_action :set_guest
  before_action :set_editor

  def agree
    content = 'agree'
    extras = {
      status: content
    }

  
    #369推送功能
    interview_id = params[:interview_id]
    interview = Interview.find_by_id(interview_id)
    user =  interview.user
    building = user.building
    editor_floor = building.editor
    editor_registration_id = editor_floor.registration_id
    village = @editor.village
    editor_door = village.editors.with_role(:door).first
    editor_estate = village.editors.with_role(:estate).first

    interview.update(flag: true)
    puts interview.id
    guest = Guest.find_by_id(params[:guest_id])
    idcard = guest.idcard
    #title = "#{guest.name} 访问 #{user.name}已经超过3小时
    message = []
    intervals = [{time: 3, kind: "3"}, {time: 6, kind: "6"}, {time: 9, kind: "9"}]
    intervals.each do |item|
          title = "访问 #{user.name} 已经超过#{item[:kind]}小时"
          extras_comment = {
            content: title,
            name: guest.name,
            nation: idcard.nation,
            card_code: idcard.code,
            photo: idcard.photo.url,
            door_name: interview.door_name,
            entered_at: interview.entered_at.to_s,
            kind: item[:kind],
            floor_code: user.floor_code, 
            building_code: user.building.code,
            village_name: user.village.name,
            user_telephone: user.telephone
          }
          puts "time"
          puts item[:time].minute.class
          if editor_floor.nil? or editor_registration_id.blank?
            message.push "没有楼主管理员极光推送ID"
          else
            #JpushServiceJob.set(wait: item[:time].hours).perform_later(interview_id,editor_registration_id,extras_comment,title)
          end
          if editor_door.nil? or editor_door.registration_id.blank?
             message.push "没有门卫极光推送ID"
          else
            #JpushServiceJob.set(wait: item[:time].hours).perform_later(interview_id,editor_door.registration_id,extras_comment,title)
          end
          puts message
          if editor_estate.nil? or  editor_estate.registration_id.blank?
            message.push "没有片区民警极光推送ID"
          else
            puts 'delay'
            puts JpushServiceJob.set(wait: 9.hours)
            puts interview_id,editor_estate.registration_id,extras_comment,title
            #JpushServiceJob.set(wait: item[:time].hours).perform_later(interview_id,editor_estate.registration_id,extras_comment,title)
          end
          puts extras_comment
    end
    puts "door"
    puts editor_door.class
    puts editor_door
    puts message
    JpushService.new(@editor.registration_id, extras, content).invoke

    render  :show
    #head 201
  end

  def disagree
    content = 'disagree'
    extras = {
      status: content
    }
    interview_id = params[:interview_id]
    interview = Interview.find_by_id(interview_id)
    interview.update(flag: false)
    JpushService.new(@editor.registration_id, extras, content).invoke

    render  :show
    #head 201
  end

  private

  def set_guest
    @guest = Guest.find(params[:guest_id])
  end

  def set_editor
    @editor = Editor.find(params[:editor_id])
  end
end
